"""
    登录自己的账号，失败则随机登陆.
    v0.1
        1.登录自己的账号；失败则随机登陆.
    v0.2
        1.登录自己的账号；失败则随机登陆.
        2.集成IP的DHCP与静态（10.21.1.2）之间的更改。
"""

from requests import get, post
from re import search
from os import system
from time import sleep
from myPy.allConnect import start
from myPy.get_UCA import getUAC

post_information = {
    '0MKKey': '123456',
    'buttonClicked': '',
    'cmd': '',
    'DDDDD': '201613010215@cmcc',
    'err_flag': '',
    'Login': '',
    'para': '00',
    'password': '',
    'R1': '0',
    'R2': '',
    'R3': '0',
    'R6': '0',
    'redirect_url': '',
    'upass': '063918',
    'user': '',
    'username': ''}
hander_Dict = {
    'Accept': ' text/html, application/xhtml+xml, image/jxr, */*',
    'Accept-Encoding': ' gzip, deflate',
    'Accept-Language': ' zh-Hans-CN, zh-Hans; q=0.8, en-GB; q=0.5, en; q=0.3',
    'Cache-Control': ' no-cache',
    'Connection': ' Keep-Alive',
    'Content-Length': ' 143',
    'Content-Type': ' application/x-www-form-urlencoded',
    'Host': ' 172.30.4.129',
    'Referer': ' http',
    "User-Agent": ' Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}
url_post = r"http://172.30.4.129/a70.htm?isReback=1"
url_loginOut = r"http://172.30.4.129/F.htm"
url_host = r"http://172.30.4.129/"


def initialize():
    """
    查询当前登录状态
    已登录返回：1，否则返回：0
    """
    url = get(url_host, timeout=2)
    url.encoding = "gbk"
    url = url.text
    BOOL = search("注销页", url, )
    if BOOL is not None: return True
    else: return False


def login():
    """ 登录成功返回1 反之返回0 """
    p = post(url=url_post, data=post_information, timeout=3)
    if p.status_code == 200: return initialize()
    return 0


def static():
    system('echo 开始设置静态IP &&\
            cmd /k netsh interface ip set address name="WLAN" source=static addr=10.21.1.2  mask=255.255.248.0 gateway=10.21.0.1 &&\
            cmd /k netsh interface ip set DNS "WLAN" static  58.20.127.238 validate=no &&\
            cmd /k netsh interface ip add DNS "WLAN" 58.20.127.170 validate=no &&\
            ping -n 3 -w 1000 10.21.0.1 > nul')
    return 


def dhcp():
    """"""
    system('echo WLAN的动态IP（DHCP）设置中... &&\
            netsh interface ip set address "WLAN" dhcp &&\
            netsh interface ip set dns "WLAN" dhcp && ping -n 3 -w 1000 10.21.0.1')
    return


def changeIp() -> object:
    button = input("1:静态IP；   2：DHCP")
    if button[0] == '1': getUAC(static)
    elif button[0] == '2': getUAC(dhcp)
    else: changeIp()
    return


if __name__ == "__main__":
    # button = initialize()
    # login()
    # start()
    print("ForMe.py Test 2")
    changeIp()
    if not initialize():
        print("账号未登录，正在登录...")
        if not login():
            print("登录失败，正在执行随即登录...")
            sleep(3)
            start()
    print("已处于登录状态，3s后退出。。。")
    sleep(3)
